import React, { PropTypes } from 'react'
import { routerRedux } from 'dva/router'
import { connect } from 'dva'
import { Spin } from 'antd'
import GoodsSearch from '../components/update/goods-search'
import UpdateInfoList from '../components/update/goodsinfolist'


//商品信息更新
function GoodsInfo ({location,dispatch,app,updateGoodsInfo}) {
	const {list,loading,pagination,modalVisible} = updateGoodsInfo

	const updateListProps = {
	    dataSource: list,
	    loading,
	    pagination: pagination,
	    onOk(data){
				dispatch({
					type: 'updateGoodsInfo/update',
					payload: data
				})
			},
			onCancel() {
				dispatch({
					type: 'updateGoodsInfo/hideModal'
				})
			},
	    onPageChange (page) {
		      const { query, pathname } = location;
		      dispatch(routerRedux.push({
		        pathname: pathname,
		        query: {
		          ...query,
		          page: page.current,
		          pageSize: page.pageSize
		        }
		      }))
	    },
	    onDeleteItem(id) {
			dispatch({
				type: 'updateGoodsInfo/delete',
				payload: id
			})
		},
		onEditItem(item) {
			dispatch({
				type: 'updateGoodsInfo/showModal',
				payload: {
					modalType: 'update',
					currentItem: item
				}
			})
		},
	}
	const goodsSearchProps={
			onSearch(code){
				const {	query,pathname} = location; 
				query.code = code;
				history.pushState(query, 'TS', 'http://127.0.0.1:8000/main-dev.html#' + pathname)
				dispatch({
						type: 'updateGoodsInfo/search',
						payload: {
							code: code
						}
					})
			},
		onRefresh(){
			dispatch({
					type: 'updateGoodsInfo/update'
				})
		},
		onGoodsIn(params){
				dispatch({
						type: 'updateGoodsInfo/create',
						payload: {...params}
					})
			},
	}

	return(
		<div className='content-inner'>
	      <Spin tip='loading...' spinning={loading} size='large'>
					<GoodsSearch {...goodsSearchProps}/>
	        <UpdateInfoList {...updateListProps} />
	      </Spin>
	    </div>
	)
}

GoodsInfo.propTypes = {
  updateGoodsInfo: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func
};

function mapStateToProps({app,updateGoodsInfo}){
	return {app,updateGoodsInfo}
}

export default connect(mapStateToProps)(GoodsInfo)